<?php


namespace LazadaApi\Services;


class LazadaSellerService extends BaseLazadaService
{
    /**
     * 获取当前卖家信息
     *
     * @see https://open.lazada.com/doc/api.htm?spm=a2o9m.11193531.0.0.2c3d6bbe6GdZ0B#/api?cid=2&path=/seller/get
     * @return mixed
     * @throws \Exception
     */
    public function getSeller()
    {
        return $this->get('/seller/get');
    }

    /**
     * 获取当前卖家的卖家指标数据，例如卖家好评率，准时发货率等
     *
     * @see https://open.lazada.com/doc/api.htm?spm=a2o9m.11193531.0.0.2c3d6bbe6GdZ0B#/api?cid=2&path=/seller/metrics/get
     *
     * @return mixed
     * @throws \Exception
     */
    public function getSellerMetricsById()
    {
        return $this->get('/seller/metrics/get');
    }

    /**
     * 更新当前卖家邮箱信息
     *
     * @see https://open.lazada.com/doc/api.htm?spm=a2o9m.11193531.0.0.2c3d6bbe6GdZ0B#/api?cid=2&path=/seller/update
     *
     * @param string $email new.email@mailinator.com
     *
     * @return mixed
     * @throws \Exception
     */
    public function updateSeller(string $email)
    {
        $sellerXmlObj = [
            'Seller' => [
                'Email' => $email
            ]
        ];
        $payload      = $this->generateXMLPayload($sellerXmlObj);
        $this->addApiParam('payload', $payload);

        return $this->post('/seller/update');
    }

    /**
     * 使用此API更新卖方帐户下用户的电子邮件地址
     *
     * @see https://open.lazada.com/doc/api.htm?spm=a2o9m.11193531.0.0.2c3d6bbe6GdZ0B#/api?cid=2&path=/user/update
     *
     * @param string $currentEmail new.email@mailinator.com
     * @param string $newEmail     current.email@mailinator.com
     *
     * @return mixed
     * @throws \Exception
     */
    public function updateUser(string $currentEmail, string $newEmail)
    {
        if ($currentEmail === $newEmail) {
            return true;
        }
        if (empty($newEmail) || empty($currentEmail)) {
            throw new \Exception('Email Data not allow empty.', 400);
        }

        $userXmlObj = [
            'User' => [
                'NewEmail' => $newEmail,
                'OldEmail' => $currentEmail
            ]
        ];
        $payload    = $this->generateXMLPayload($userXmlObj);
        $this->addApiParam('payload', $payload);

        return $this->post('/user/update');
    }
}